//logs.js
var app = getApp();
import Api from '../../../api/api.js';
const api = new Api();
Page({
  data: {
    dataList: [1, 5, 10, 20, 50, 100, 200, 500, 1000],
    selectedIndex:6,
    dsText:'确定打赏(200元)',
    money:"200",
    isCanDo:true,
    contentId:"",
    type: "5",

  },
  onLoad: function (options) {
    this.data.contentId = options.contentId;
  },
  itemClick(e){
    var index = e.currentTarget.dataset.key;
    var text = this.data.dataList[index];
    this.data.money = text+"";
    text = '确定打赏(' + text+'元)';
    this.setData({
      selectedIndex: index,
      dsText: text
    });
  },
  otherClick(){
    this.setData({
      selectedIndex: -1
    });
  },
  inputMoney(e){
    var value = e.detail.value;
    this.data.money = value + "";
    var text = '确定打赏(' + value + '元)';
    this.setData({
      dsText: text
    });
  },
  isPositiveInteger(s) {
    var re = /^[0-9][0-9]*([.][0-9]+)?$/;
    return re.test(s)
  },
  upOrder() {
    if (!this.isPositiveInteger(this.data.money)){
      app.alert("打赏金额格式输入错误~");
      return;
    }
    var floatMoney = parseFloat(this.data.money).toFixed(2);
    if (floatMoney < 0.01){
      app.alert("打赏金额要大于0.01元~");
      return;
    }
    if (!this.data.isCanDo) {
      return;
    }
    var _this = this;
    var map = {};
    map.amount = this.data.money;
    map.contentId = this.data.contentId;
    map.openId = app.OPEN_ID();
    map.type = this.data.type;
    map.userId = app.USER_ID();

    this.data.isCanDo = false;
    api.getRequestData(app.rewardPayUrl, map, false).then(res => {
      if (res.data.errorCode == 0) {
        _this.beginPay(res.data.model.wxPayMpOrderResult,res.data.model.orderNo);
      } else {
        _this.data.isCanDo = true;
        wx.showToast({
          title: res.data.errorMsg,
          icon: 'none',
          duration: 1500
        });
      }
    }).catch(err => {
      wx.hideLoading();
      _this.data.isCanDo = true,
        console.log(err);
    });
  },
  payOrder(orderId) {
    var _this = this;
    var map = {};
    map.ORDER_ID = orderId;
    api.getRequestData(app.payOrderUrl, map, false).then(res => {
      _this.data.isCanDo = true;

      if (res.data.flag) {
        _this.beginPay(res.data.result, orderId);
      } else {
        _this.data.isCanDo = true;
        app.alert(res.data.msg);
      }
    }).catch(err => {
      _this.data.isCanDo = true;
      console.log(err);
    });
  },
  beginPay: function(data, orderId) {
    console.log(JSON.stringify(data));
    var _this = this;
    wx.requestPayment({
      timeStamp: data.timeStamp,
      nonceStr: data.nonceStr,
      package: data.packageValue,
      signType: data.signType,
      paySign: data.paySign,
      success: function(res) {
        app.alert("谢谢你的支持~");
        setTimeout(() => {
          wx.navigateBack({
            delta:1,
          })
        }, 1400);
      },
      fail: function(res) {
        console.log(JSON.stringify(res));
        _this.data.isCanDo = true;
        app.alert("支付失败");
      },
      complete: function(res) {
      
      }
    })
  },
  payOrderNotify(orderId) {
    var map = {};
    api.getRequestDataNoLoading(app.payNofityUrl, map, false,"POST").then(res => {
      console.log("回调成功了啊！！！！！！！！！！！！！！！");
    }).catch(err => {});
  },   
})
